package com.busionline.wotu.baselibrary.mvp;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Created by admin on 2018/1/18.
 */

public abstract class BasePresenter<V extends IBaseView> {
    protected V mView;
    protected V mProxyView;

    public void attach(V view) {
        this.mView = view;
        //动态代理(1.当activity没有实现响应的View，而persenter调用方法时会报错 2.当activity已经销毁，而persenter仍然在调用时会报错)
        mProxyView = (V) Proxy.newProxyInstance(view.getClass().getClassLoader(), view.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                if (mView != null) {
                    return method.invoke(mView, args);
                }
                return null;
            }
        });
    }

    public void deAttach() {
        mView = null;
    }

    public V getView(){
        return mProxyView;
    }
}
